
package cfar.actors.distributed;

import cfar.actors.Message;
import cfar.actors.RemoteActor;
import cfar.ff.data.Assocs.Assoc2;
import cfar.ff.data.Maybe;
import cfar.ff.fun.F0;
import cfar.utils.IO;

import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Intended to be spawned by an ActorServer. Accepts an F0, evaluates it, returns the result
 * @author badi
 */
public class ActorF0 extends RemoteActor {

    @Override
    protected void cleanup() {
        // nothing to see here, move along
    }

    @Override
    protected void act(Message msg, Socket client) {
        if(msg.signal instanceof Signal.WorkF0){
            F0 f = (F0) msg.payload;
            Object r =  f.call();
            Assoc2<String, Object> res = new Assoc2<String, Object>(my_address_string, r);
            try {
                IO.write_object(new Message(Signal.Result, res), client.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(ActorF0.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else { unknown_signal(msg); }
    }

}
